package com.gxa.learning.pc.controller;

import com.github.pagehelper.PageInfo;
import com.gxa.learning.core.common.ExceptionCode;
import com.gxa.learning.core.common.JsonResult;
import com.gxa.learning.core.exception.BusinessException;
import com.gxa.learning.core.pojo.Question;
import com.gxa.learning.core.pojo.Template;
import com.gxa.learning.pc.service.TemplateService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 操作问卷问题的接口
 *
 * @Author ChenJ
 * @date 2021/12/02 16:12
 **/
@RestController
@RequestMapping("/template")
@Api(value = "TemplateController",tags = "操作问卷题目的接口")
public class TemplateController {

    @Autowired
    private TemplateService templateService;

    @PostMapping("/insert")
    @ApiOperation("添加问卷题目的接口")
    @ApiImplicitParam(name = "question", value = "question", paramType = "body", dataTypeClass = Template.class)
    public Map<String, Object> insert(@RequestBody Template template) throws BusinessException {

        // 数据校验
        if (template.getQuestionnaireId() == null) {
            throw new BusinessException(ExceptionCode.WRONG_REQUIRED);
        }

        // 题目的添加操作
        Template insert = templateService.insert(template);

        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setMsg("成功");
        return jsonResult.getDataMap();
    }

    @PostMapping("/getList")
    @ApiOperation("获取问卷题目列表的接口（包含模糊查询）")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "page", paramType = "query", dataType = "Integer"),
            @ApiImplicitParam(name = "size", value = "size", paramType = "query", dataType = "Integer"),
            @ApiImplicitParam(name = "questionnaireId", value = "questionnaireId", paramType = "query", dataType = "Long")
    })
    public Map<String, Object> getList(@RequestParam Integer page, @RequestParam Integer size, @RequestParam Long questionnaireId) throws BusinessException, IllegalAccessException {

        // 数据校验
        if (questionnaireId == null) {
            throw new BusinessException(ExceptionCode.WRONG_REQUIRED);
        }
        if (page == null) {
            page = 1;
        }
        if (size == null) {
            size = 10;
        }

        Template template = new Template();
        // 设置查询规则
        template.setQuestionnaireId(questionnaireId);
        PageInfo<Template> questionPageInfo = templateService.selectByPage(page, size, template);
        List<Template> list = questionPageInfo.getList();
        if (list.size() == 0) {
            throw new BusinessException(ExceptionCode.NO_QUESTION);
        }
        long total = questionPageInfo.getTotal();

        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setMsg("成功");
        jsonResult.setData(list);
        jsonResult.put("count", total);
        return jsonResult.getDataMap();
    }

    @PostMapping("/delete")
    @ApiOperation("删除问卷题目的接口")
    @ApiImplicitParam(name = "question", value = "question", paramType = "query", dataTypeClass = Question.class)
    public Map<String, Object> delete(@RequestParam Long id) throws BusinessException {

        // 数据校验
        if (id == null) {
            throw new BusinessException(ExceptionCode.WRONG_REQUIRED);
        }

        Question question = new Question();
        // 设置删除条件
        question.setId(id);

        Integer integer = templateService.deleteById(question);
        if (integer == 0) {
            throw new BusinessException(ExceptionCode.WRONG_DELETE);
        }

        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setMsg("成功");
        return jsonResult.getDataMap();
    }
}
